#ifndef LEMC_TEXTFIELD_H
#define LEMC_TEXTFIELD_H

#include "WindowsIncludes.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <string>
#include <functional>

#include "guielement.h"
#include "../resourcemanager/allegro_bitmap_manager.h"
#include "../system/system.h"
#include "../system/global.h"

/*
* Provides the functionality of a simple textfield that contains user input.
*/
class TextField : public GuiElement
{
private:
	// The graphic id of the graphic to show when the field is active.
	std::string m_graphic_active;

	// The graphic id of the graphic to show when the field is inactive.
	std::string m_graphic_inactive;

	// The font of the text.
	ALLEGRO_FONT* m_text_font;

	// The color of the text.
	ALLEGRO_COLOR m_text_color;

	// The distance between graphic border and the text.
	int m_padding;

	// The maximum number of characters that the text field can contain.
	unsigned m_maxchars;
	
	// True if the textfield can currently receive input.
	bool m_active;

	// The action to be triggered when the text is confirmed (e.g. via enter button).
	std::function<void(std::string)> m_action;

	// The text that has been written into the field.
	std::string m_text;

public:
	/*
	* Creates the Text field.
	*
	* @param id The id of the textfield, should be unique.
	* @param x The x position of the upper left corner of the textfield.
	* @param y The y position of the upper left corner of the textfield.
	* @param size_x Horizontal size of the textfield.
	* @param size_y Vertical size of the textfield.
	* @param action The action to be triggered when the text is confirmed (e.g. via enter button).
	* @param text_font The font of the text within the field.
	* @param text_color The color of the text within the field.
	* @param padding The distance between graphic border and the text.
	* @param maxchars The maximum number of characters that can be contained.
	* @param graphic_active The graphic to show when the textfield is active.
	* @param graphic_inactive The graphic to show when the textfield is inactive.
	*/
	TextField(std::string id, int x, int y, int size_x, int size_y, std::function<void(std::string)> action,
		ALLEGRO_FONT* text_font, const unsigned char text_color[3] = STANDARD_TEXTFIELD_TEXT_COLOR,
		int padding = STANDARD_TEXTFIELD_PADDING, unsigned maxchars = STANDARD_TEXTFIELD_MAXCHARS, 
		std::string graphic_active = STANDARD_TEXTFIELD_GRAPHIC_ACTIVE,
		std::string graphic_inactive = STANDARD_TEXTFIELD_GRAPHIC_INACTIVE);
	~TextField();

	void PrimaryMouseButtonDown(int x, int y);
	void CharEntered(int keycode, int unichar);

	void DeleteText();

	void Draw();
};

#endif